# -*- encoding: utf-8 -*-
"""
File Name: elastic_client
Description:
Author : 'lirui'
date: 2024/1/9
Contact: marjey_lee@163.com
Change Activity:
2024/1/9:
"""
import urllib3
from elasticsearch import Elasticsearch

import ll

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class ElasticClient:
    def __init__(
            self,
            url,
            username='network',
            password='P@ssword132'
    ):
        self.url = url
        self.username = username
        self.password = password
        self.client = None
        self.refresh_connect()

    def check_is_connected(self):
        return self.client.ping

    def refresh_connect(self):
        self.client = Elasticsearch(
            self.url,
            http_auth=(self.username, self.password),
            verify_certs=False
        )

    def refresh_client(self):
        self.client = Elasticsearch(
            self.url,
            http_auth=(self.username, self.password),
            verify_certs=False
        )

    def close(self):
        self.client.close()

    def create_index(self, index_name):
        """

        Args:
            index_name:

        Returns:

        """

        return self.client.indices.create(index=index_name)

    def delete_index(self, index_name):
        """

        Args:
            index_name:

        Returns:

        """
        ll.get_logger().info(f'Delete index {index_name}.')
        self.client.indices.delete(index=index_name)

    def delete_matched_indexes(self, index_name):
        """

        Args:
            index_name:

        Returns:

        """
        indexes = self.get_all_index()
        body = indexes.body
        for i in body:
            name = i['index']
            if index_name in name:
                self.delete_index(
                    index_name=name
                )

    def get_all_index(self):
        """

        Returns:

        """
        return self.client.cat.indices(format='json')

    def get_match_index(self, prefix):
        match_index = []
        all_index = self.client.cat.indices(format='json')
        for index in all_index:
            if str(index['index']).startswith(prefix):
                match_index.append(index)
        return match_index

    def get_match_index_name(self, prefix):
        match_index = []
        all_index = self.client.cat.indices(format='json')
        for index in all_index:
            if str(index['index']).startswith(prefix):
                match_index.append(index['index'])
        return match_index

    def insert(self, index, doc_id, doc):
        """

        Args:
            index:
            doc_id:
            doc:

        Returns:

        """
        return self.client.index(index=index, id=doc_id, document=doc)

    def select_by_id(self, index, doc_id):
        return self.client.get(index=index, id=doc_id)

    def search(
            self,
            index_lst,
            query
    ):
        return self.client.search(index=index_lst, query=query)

    def update(self, index, doc_id, doc):
        """

        Args:
            index:
            doc_id:
            doc:

        Returns:

        """
        return self.client.update(index=index, id=doc_id, doc=doc)

    def delete(self, index, doc_id):
        self.client.delete(index=index, id=doc_id)
